package com.pakazaka.simplecrawler;

import com.pakazaka.enemies.BasicEnemy;
import com.pakazaka.layer.TiledLayer;
import com.pakazaka.layer.tiles.Tile;
import static com.pakazaka.map.SimpleGenerate.generateMap;
import com.pakazaka.player.Player;
import com.pakazaka.roguelike.ui.StatsWindow;
import com.pakazaka.roguelike.ui.FSInventory;
import com.pakazaka.roguelike.ui.InGameConsole;
import static com.pakazaka.simplecrawler.SimpleCrawler.maph;
import static com.pakazaka.simplecrawler.SimpleCrawler.mapw;
import com.pakazaka.testItemsSystem.Item;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import static javax.imageio.ImageIO.read;

/**
 * @author Remi
 */
public class Core extends JPanel {

    public static boolean debug = false;
    public StatsWindow window1 = new StatsWindow(100, 100, 50, 50, 2, "Window 1");
    public StatsWindow window2 = new StatsWindow(200, 150, 200, 170, 1, "Window 2");
    protected ArrayList<Item> itemsOnMap = new ArrayList<>();
    protected ArrayList<Window> windows = new ArrayList<>();
    protected ArrayList<BasicEnemy> enemies = new ArrayList<>();
    private TiledLayer layer;
    private final Player player = new Player("/res/hero.png");
    private final FSInventory inventory;
    //used for action hints
    private String whereWessage = new String();
    private final InGameConsole console = new InGameConsole();
    //overview coordinates
    private int overviewModeX = 0, overviewModeY = 0;
    private static Core instance;

    private Core(String tile_path, int w, int h, Dimension TILE_SIZE) {
        try {
            layer = new TiledLayer(read(getClass().getResourceAsStream(tile_path)), TILE_SIZE.width, TILE_SIZE.height, w, h);
        } catch (IOException ex) {
        }

        try {
            generateMap(layer, itemsOnMap);
        } catch (CloneNotSupportedException e) {
            System.out.println("clone not supported lel");
        }

        windows.add(window1);
        windows.add(window2);

        addMouseListener(window1);
        addMouseListener(window2);

        BasicEnemy e1 = new BasicEnemy("/res/enemy.png");
        e1.setAbsoluteBlocksX(12);
        e1.setAbsoluteBlocksY(11);
        enemies.add(e1);

        player.setBlocksX(12);
        player.setBlocksY(12);
        console.pushMessage("New MOTD in English. Switch keyboard layout to ENG, plz.");
        console.pushMessage("Meow.");

        inventory = new FSInventory(player.getItems());
    }

    public ArrayList<BasicEnemy> getEnemies() {
        return enemies;
    }

    public void setEnemies(ArrayList<BasicEnemy> enemies) {
        this.enemies = enemies;
    }

    public static Core getInstance() {
        if (instance == null) {
            instance = new Core("/res/tiles.png", mapw, maph, Tile.SIZE);
        }
        return instance;
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        if (!inventory.isPaint()) {
            g2d.setColor(Color.black);
            g2d.fillRect(0, 0, SimpleCrawler.getWindows()[0].getWidth(), SimpleCrawler.getWindows()[0].getHeight());

            layer.paintLayer(g);

            //drawing items on map
            for (Item item : itemsOnMap) {
                //some ХИТРОВЫЕБАННОЙ МАТЕМАТИКИ
                final int x = Math.abs(item.getBlocksX() - player.getAbsoluteBlocksX());
                final int y = Math.abs(item.getBlocksY() - player.getAbsoluteBlocksY());
                int limitX = 12;
                if (player.getBlocksX() < 12) {
                    limitX += (limitX - player.getBlocksX());
                } else if (player.getBlocksX() > 12) {
                    limitX += (player.getBlocksX() - limitX);
                }

                if (x <= limitX && y <= 24) {
                    item.paint(g, SimpleCrawler.all.getImage(item.getImageId()));
                }
            }

            //drawing enemies in map
            for (BasicEnemy e : enemies) {
                e.paint(g);
            }

            player.processPockets();
            player.update();
            player.updateEffects();
            player.paint(g);
        }
        if (inventory.isPaint()) {
            inventory.paint(g);
        } else {
            console.paint(g);
        }

        //draw rect around game area
        if (!inventory.isPaint()) {
            g2d.setColor(Color.white);
            g.drawString(whereWessage, 2, 12);
            g2d.drawRect(0, 0, 300, 400);
        }

        //painting windows
        for (Window w : windows) {
            //w.paint(g, w);
        }

    }

    public Player getPlayer() {
        return player;
    }

    public TiledLayer getLayer() {
        return layer;
    }

    public ArrayList<Item> getItemsOnMap() {
        return itemsOnMap;
    }

    public ArrayList<Window> getWindows() {
        return windows;
    }

    public void setWhereMessage(String msg) {
        whereWessage = msg;
    }

    public InGameConsole getConsole() {
        return console;
    }

    public int getOverviewX() {
        return overviewModeX;
    }

    public void setOverviewX(int x_over) {
        this.overviewModeX = x_over;
    }

    public int getOverviewY() {
        return overviewModeY;
    }

    public void setOverviewY(int y_over) {
        this.overviewModeY = y_over;
    }

    public FSInventory getInventory() {
        return inventory;
    }

    //public boolean isPaintInventory() {
    //     return paintInventory;
    //}
    //public void setPaintInventory(boolean state) {
    //    paintInventory = state;
    //    
    //}
}
